﻿using Md.Api.Entity.ModelDB;
using PetaPoco.NetCore;

namespace Md.Petapoco.Repositories.MdTrainingEducation;
/// <summary>
/// 系统参数配置仓储
/// </summary>
public class SysConfigRep : BaseRep<SysConfigModel>
{
    /// <summary>
    /// 通过配置编号获取系统配置信息
    /// </summary>
    /// <param name="configId">配置编号</param>
    /// <returns>SysConfigModel</returns>
    public SysConfigModel GetSysConfigByConfigId(int configId)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `config_id`
                  ,`config_name`
                  ,`config_key`
                  ,`config_value`
                  ,`config_type`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_config`
            where 1 = 1 and `config_id` = @0
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<SysConfigModel>(sqlStr, configId);
    }
    /// <summary>
    /// 通过配置KEY获取系统配置信息
    /// </summary>
    /// <param name="configKey">配置KEY</param>
    /// <returns>SysConfigModel</returns>
    public SysConfigModel GetSysConfigByConfigKey(string configKey)
    {
        #region ===sql===
        string sqlStr = string.Format(@"
            select `config_id`
                  ,`config_name`
                  ,`config_key`
                  ,`config_value`
                  ,`config_type`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_config`
            where 1 = 1 and `config_key` = @0 limit 1
        ");
        #endregion

        return MdTrainingEducationDB.GetInstance().SingleOrDefault<SysConfigModel>(sqlStr, configKey);
    }
    /// <summary>
    /// 获取系统配置列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns>IEnumerable<SysConfigModel></returns>
    public IEnumerable<SysConfigModel> GetSysConfigList(SysConfigModel model, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ConfigId > 0)
        {
            sqlWhere += " and config_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sqlWhere += " and config_name like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sqlWhere += " and config_key like  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sqlWhere += " and config_value like @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sqlWhere += " and config_type = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @7 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @8 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `config_id`
                  ,`config_name`
                  ,`config_key`
                  ,`config_value`
                  ,`config_type`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_config`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Query<SysConfigModel>(
            sqlStr,
            model.ConfigId,
            string.Format(@"%{0}%", model.ConfigName),
            string.Format(@"%{0}%", model.ConfigKey),
            string.Format(@"%{0}%", model.ConfigValue),
            model.ConfigType,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 分页获取系统配置列表数据
    /// </summary>
    /// <param name="model">筛选条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <param name="beginTime">开始时间</param>
    /// <param name="endTime">结束时间</param>
    /// <returns>Page<SysConfigModel></returns>
    public Page<SysConfigModel> GetSysConfigPageList(SysConfigModel model, int pageIndex, int pageSize, string? orderBy, bool? isDesc, DateTime? beginTime, DateTime? endTime)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ConfigId > 0)
        {
            sqlWhere += " and config_id = @0 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sqlWhere += " and config_name like @1 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sqlWhere += " and config_key like  @2 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sqlWhere += " and config_value like @3 ";
        }
        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sqlWhere += " and config_type = @4 ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and create_by = @5 ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and update_by = @6 ";
        }
        if (beginTime.HasValue)
        {
            sqlWhere += " and create_time >= @7 ";
        }
        if (endTime.HasValue)
        {
            sqlWhere += " and create_time <= @8 ";
        }
        #endregion

        #region ===sql===
        string sqlStr = string.Format(@"
            select `config_id`
                  ,`config_name`
                  ,`config_key`
                  ,`config_value`
                  ,`config_type`
                  ,`create_by`
                  ,`create_time`
                  ,`update_by`
                  ,`update_time`
                  ,`remark`
            from `sys_config`
            where 1 = 1 {0} {1}
         ", sqlWhere, GetOrderByDesc(orderBy, isDesc));
        #endregion

        return MdTrainingEducationDB.GetInstance().Page<SysConfigModel>(
            pageIndex,
            pageSize,
            sqlStr,
            model.ConfigId,
            string.Format(@"%{0}%", model.ConfigName),
            string.Format(@"%{0}%", model.ConfigKey),
            string.Format(@"%{0}%", model.ConfigValue),
            model.ConfigType,
            model.CreateBy,
            model.UpdateBy,
            string.Format(@"{0} 00:00:00", beginTime),
            string.Format(@"{0} 23:59:59", endTime));
    }
    /// <summary>
    /// 通过配置编号更新系统配置
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>int</returns>
    public long UpdateSysConfigByConfigId(SysConfigModel model)
    {
        var sql = new Sql();

        sql.Append(" update `sys_config` set ");

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.ConfigName))
        {
            sql.Append(" config_name = @0, ", model.ConfigName);
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigKey))
        {
            sql.Append(" config_key = @0, ", model.ConfigKey);
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigValue))
        {
            sql.Append(" config_value = @0, ", model.ConfigValue);
        }

        if (!string.IsNullOrWhiteSpace(model.ConfigType))
        {
            sql.Append(" config_type = @0, ", model.ConfigType);
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sql.Append(" update_by = @0, ", model.UpdateBy);
        }

        if (!string.IsNullOrWhiteSpace(model.Remark))
        {
            sql.Append(" remark = @0, ", model.Remark);
        }

        sql.Append(" update_time = sysdate() ");
        #endregion

        sql.Append(" where 1 = 1 and config_id = @0 ", model.ConfigId);

        return MdTrainingEducationDB.GetInstance().Execute(sql);
    }
}

